/*
 * The MIT License
 *
 * Copyright 2015 Gavin Crayk.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.googlecode.census.client.stream.response;

import com.google.gson.annotations.SerializedName;
import com.googlecode.census.client.EventType;
import com.googlecode.census.client.Service;
import com.googlecode.census.client.Worlds;
import org.apache.commons.lang3.builder.StandardToStringStyle;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This is an immutable instance of a message that is periodically sent by the
 * census API to indicate the current server status.
 *
 * <pre>
 * {
 *  "online" : {
 *      "EventServerEndpoint_Briggs_25":"true",
 *      "EventServerEndpoint_Cobalt_13":"true",
 *      "EventServerEndpoint_Connery_1":"true",
 *      "EventServerEndpoint_Emerald_17":"true",
 *      "EventServerEndpoint_Jaeger_19":"true",
 *      "EventServerEndpoint_Miller_10":"true"
 *  },
 *  "service" : "event",
 *  "type" : "heartbeat"
 * }
 * </pre>
 *
 * @author Gavin Crayk
 */
public final class HeartbeatResponse {

    private static final Logger LOG = LoggerFactory.getLogger(HeartbeatResponse.class);

    private class Online {

        @SerializedName("EventServerEndpoint_Briggs_25")
        private final Boolean briggsAlive = Boolean.FALSE;
        @SerializedName("EventServerEndpoint_Cobalt_13")
        private final Boolean cobaltAlive = Boolean.FALSE;
        @SerializedName("EventServerEndpoint_Connery_1")
        private final Boolean conneryAlive = Boolean.FALSE;
        @SerializedName("EventServerEndpoint_Emerald_17")
        private final Boolean emeraldAlive = Boolean.FALSE;
        @SerializedName("EventServerEndpoint_Jaeger_19")
        private final Boolean jaegerAlive = Boolean.FALSE;
        @SerializedName("EventServerEndpoint_Miller_10")
        private final Boolean millerAlive = Boolean.FALSE;

        public boolean isOnline(final Worlds world) {
            boolean result = false;
            switch (world) {
                case BRIGGS:
                    result = briggsAlive;
                    break;
                case COBALT:
                    result = cobaltAlive;
                    break;
                case CONNERY:
                    result = conneryAlive;
                    break;
                case EMERALD:
                    result = emeraldAlive;
                    break;
                case JAEGER:
                    result = jaegerAlive;
                    break;
                case MILLER:
                    result = millerAlive;
                    break;
            }

            return result;
        }

        @Override
        public String toString() {
            return new ToStringBuilder(this, StandardToStringStyle.SHORT_PREFIX_STYLE)
                    .append("Briggs", briggsAlive)
                    .append("Cobalt", cobaltAlive)
                    .append("Connery", conneryAlive)
                    .append("Emerald", emeraldAlive)
                    .append("Jaeger", jaegerAlive)
                    .append("Miller", millerAlive)
                    .toString();
        }
    }

    private Online online;
    private EventType type;
    private Service service;

    private HeartbeatResponse() {
    }

    public boolean isOnline(final Worlds world) {
        return online.isOnline(world);
    }

    public EventType getType() {
        return type;
    }

    public Service getService() {
        return service;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, StandardToStringStyle.SHORT_PREFIX_STYLE)
                .append("EventType", type)
                .append("Service", service)
                .append("Online", online)
                .toString();
    }
}
